package cn.wikiflyer.beikang.act;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.home.HomeAct;
import cn.wikiflyer.beikang.act.product.ProductInfoAct;
import cn.wikiflyer.beikang.act.study.bean.CategoryBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

/**
 * 主Tab
 */
@SuppressWarnings("deprecation")
public class TabAct extends TabActivity {

	private TabHost tabHost;

	private TextView tab_msg;

	private ImageView tab_img_1;
	private ImageView tab_img_2;
	private ImageView tab_img_3;
	private ImageView tab_img_4;
	private ImageView tab_img_5;

	private TextView tab_txt_1;
	private TextView tab_txt_2;
	private TextView tab_txt_3;
	private TextView tab_txt_4;
	private TextView tab_txt_5;

	private ImageView[] tabImg;
	private TextView[] tabTxt;
	private int[] imgIds;
	private int[] imgSelectedIds;

	public static TabAct self = null;

	public IApplication app() {
		return (IApplication) getApplicationContext();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.bk_act_tab);
		self = this;
		// 定位初始化
		app().location.startOnce();

		tab_msg = (TextView) super.findViewById(R.id.tab_msgnum);

		// CheckAppUpdate.getInstance().check(this);

		tabHost = this.getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, HomeAct.class);
		spec = tabHost.newTabSpec("TabHomeAct").setIndicator("TabHomeAct")
				.setContent(intent);
		tabHost.addTab(spec);

		// intent = new Intent().setClass(this, TabShopAct.class);
		intent = new Intent().setClass(this, TabStudyAct.class);
		spec = tabHost.newTabSpec("TabStudyAct").setIndicator("TabStudyAct")
				.setContent(intent);
		tabHost.addTab(spec);

		// 是否只有一个商品
		if (IApplication.isOnlyOneProduct) {
			intent = new Intent().setClass(this, ProductInfoAct.class);
		} else {
			intent = new Intent().setClass(this, ProductAct.class);
		}
		spec = tabHost.newTabSpec("TabProductAct")
				.setIndicator("TabProductAct").setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, CartAct.class);
		spec = tabHost.newTabSpec("TabShopAct").setIndicator("TabShopAct")
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, TabUserAct.class);
		spec = tabHost.newTabSpec("TabUserAct").setIndicator("TabUserAct")
				.setContent(intent);
		tabHost.addTab(spec);
		tabHost.setCurrentTab(0);

		clickView = findViewById(R.id.clickView);

		RadioGroup radioGroup = (RadioGroup) this
				.findViewById(R.id.tab_rg_menu);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				clickView.setClickable(true);
				handler.sendMessageDelayed(new Message(), 100);
				switch (checkedId) {
				case R.id.tab_rb_0:
					tabHost.setCurrentTabByTag("TabHomeAct");
					clickTabBtn(0);
					break;
				case R.id.tab_rb_1:
					tabHost.setCurrentTabByTag("TabStudyAct");
					clickTabBtn(1);
					break;
				case R.id.tab_rb_2:
					tabHost.setCurrentTabByTag("TabProductAct");
					clickTabBtn(2);
					break;
				case R.id.tab_rb_3:
					tabHost.setCurrentTabByTag("TabShopAct");
					clickTabBtn(3);
					break;
				case R.id.tab_rb_4:
					tabHost.setCurrentTabByTag("TabUserAct");
					clickTabBtn(4);
					break;
				}
			}
		});

		tab_txt_1 = (TextView) findViewById(R.id.tab_txt_1);
		tab_txt_2 = (TextView) findViewById(R.id.tab_txt_2);
		tab_txt_3 = (TextView) findViewById(R.id.tab_txt_3);
		tab_txt_4 = (TextView) findViewById(R.id.tab_txt_4);
		tab_txt_5 = (TextView) findViewById(R.id.tab_txt_5);

		tab_img_1 = (ImageView) findViewById(R.id.tab_img_1);
		tab_img_2 = (ImageView) findViewById(R.id.tab_img_2);
		tab_img_3 = (ImageView) findViewById(R.id.tab_img_3);
		tab_img_4 = (ImageView) findViewById(R.id.tab_img_4);
		tab_img_5 = (ImageView) findViewById(R.id.tab_img_5);

		tabImg = new ImageView[] { tab_img_1, tab_img_2, tab_img_3, tab_img_4,
				tab_img_5 };
		tabTxt = new TextView[] { tab_txt_1, tab_txt_2, tab_txt_3, tab_txt_4,
				tab_txt_5 };
		imgIds = new int[] { R.drawable.tab1, R.drawable.tab2, R.drawable.tab3,
				R.drawable.tab4, R.drawable.tab5 };
		imgSelectedIds = new int[] { R.drawable.tab1_selected,
				R.drawable.tab2_selected, R.drawable.tab3_selected,
				R.drawable.tab4_selected, R.drawable.tab5_selected };
		clickTabBtn(0);
		// 获取学习的分类
		new GetTargetListAsyn(TabAct.this);
	}

	/**
	 * tab 点击事件
	 */
	private void clickTabBtn(int idx) {
		for (int i = 0; i < tabImg.length; i++) {
			if (i == idx) {// 选中
				tabImg[i].setImageResource(imgSelectedIds[i]);
				tabTxt[i].setTextColor(getResources().getColor(
						R.color.color_style));
			} else {// 默认状态
				tabImg[i].setImageResource(imgIds[i]);
				tabTxt[i].setTextColor(getResources().getColor(
						R.color.tab_btn_txt));
			}
		}
	}

	private View clickView;
	private Handler handler = new Handler(new Callback() {
		public boolean handleMessage(Message arg0) {
			clickView.setClickable(false);
			return false;
		}
	});

	private boolean isExit;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		}
		return super.dispatchKeyEvent(event);
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			IApplication.showToast("再按一次退出程序");
			mHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			MobclickAgent.onKillProcess(this);
			System.exit(0);
		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}
	};

	/**
	 * 设置tab，0-4
	 */
	public void setTab(int i) {
		try {
			this.tabHost.setCurrentTab(i);
			// ((RadioButton) findViewById(R.id.tab_rb_2)).setChecked(false);
			// ((RadioButton) findViewById(R.id.tab_rb_3)).setChecked(true);
			// ((RadioButton) findViewById(R.id.tab_rb_2)).setChecked(true);
			clickTabBtn(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 设置tab，0-4
	 */
	public void setTabToHome() {
		try {
			this.tabHost.setCurrentTab(0);
			((RadioButton) findViewById(R.id.tab_rb_1)).setChecked(true);
			((RadioButton) findViewById(R.id.tab_rb_2)).setChecked(false);
			clickTabBtn(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		app().initApp(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	/**
	 * 获取学习分类接口
	 */
	class GetTargetListAsyn extends WKAsyncTaskPro {
		public GetTargetListAsyn(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return ((IApplication) app()).net.getStudyCate();
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<ArrayList<CategoryBean>>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<ArrayList<CategoryBean>> base = gson.fromJson(result,
					cvbType);
			if (base.status == 1) {
				app().studyCates = base.data;
			} else {
				// Toast.makeText(c, "获取数据失败", 0).show();
			}
		}
	}
}
